package org.flexlancer.databases
{
	import flash.utils.Dictionary;
	
	public class DishCategories
	{
		public static const TABLE:String = "dishcategories";
		
		public static function Add(catname:String):void{
			try{
				var insertObj:Object = new Object();
				insertObj.catname = catname;
				DatabaseManager.Manager.Insert(TABLE,insertObj);
			}catch(err:Error){
				throw err;
			}
		}

		public static function Update(catid:Number, catname:String):void{
			try{
				var updateObj:Object = new Object();
				updateObj.catname = catname;
				DatabaseManager.Manager.Update(TABLE,updateObj,"catid="+catid);
			}catch(err:Error){
				throw err;
			}
		}
		
		public static function List(condition:String = ""):Array{
			try{
				return DatabaseManager.Manager.Select(TABLE,null,condition);
			}catch(err:Error){
				throw err;
			}
			return new Array;
		}
		
		public static function Delete(condition:String = ""):void{
			try{
				DatabaseManager.Manager.Delete(TABLE,condition);
			}catch(err:Error){
				throw err;
			}
		}
		
		public static function Menu():Array{
			try{
				var retValue:Array = List();
				
				var dtChildren:Dictionary = new Dictionary;
				
				for each ( var singleHead:Object in retValue ){
					dtChildren[singleHead.catid] = new Array;
				}
				
				for each ( var singleItem:Object in Dishes.List() ){
					var arr:Array = dtChildren[singleItem.itemcat];
					if ( arr != null ){
						arr.push(singleItem);
					}
				}
				
				for each ( var singleHead1:Object in retValue ){
					singleHead1.children = dtChildren[singleHead1.catid]; 
				}
				
				return retValue;
			}catch(err:Error){
				throw err;
			}
			return new Array;
		}
	}
}